/* eslint-disable import/no-cycle */
import "./editorCss";
import instance from "./instance";
import extendsEvent, { EventCenter } from "./EventCenter";
import panZoom from "./moveAndZoom";
import contentMenu from "./contextMenu";
import editor from "./editor";
import model from "./model";
import exec, { undo, AddNodeCommand } from "./Command";

function unique(arr) {
  var newArr = [];
  for (var i = 0; i < arr.length; i++) {
    if (newArr.indexOf(arr[i]) == -1) {
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

const FlowChart = {
  /**
   * @description 设置插件容器
   * @param {string} id 容器id
   */
  setContainer(id) {
    instance.setContainer(id);
    this.init();
  },

  /**
   * @description 在容器内添加节点
   * @param {object} position {left:0; top:0}节点位置
   * @param {string} elId 记录生产节点的源节点id，便于传递原节点信息
   */
  addNode(position, elId, type) {
    exec(AddNodeCommand, position, elId, type);
  },

  getData() {
    var data = editor.getData();
    if (data.edges.length) {
      // 去重
      data.edges = unique(data.edges);
      // 去除无效对象
      var edgesdata = {};

      for (var key in data.edgesdata) {
        if (data.edges.indexOf(key) > -1) {
          edgesdata[key] = data.edgesdata[key];
        }
      }

      data.edgesdata = edgesdata;

      data.edges.forEach(item => {
        if (!data.edgesdata[item]) {
          data.edgesdata[item] = {};
        }
      });
    }

    return data;
  },

  getDataByNodeId(nodeId) {
    let data = editor.getData();
    for (let index = 0; index < data.nodes.length; index++) {
      const element = data.nodes[index];
      if (element.id == nodeId) {
        return element.data.parameter;
      }
    }
  },

  getPointByNodeId(nodeId) {
    let data = editor.getData();
    for (let index = 0; index < data.nodes.length; index++) {
      const element = data.nodes[index];
      if (element.id == nodeId) {
        return {
          source: element.points.sources[0],
          target: element.points.targets[0]
        };
      }
    }
  },

  setDataByNodeId(nodeId, inputData) {
    let data = editor.getData();
    for (let index = 0; index < data.nodes.length; index++) {
      const element = data.nodes[index];
      if (element.id == nodeId) {
        element.data.data = inputData;
        return;
      }
    }
  },

  /**
   * @description 撤销
   */
  undo() {
    undo();
  },

  /**
   * @description 初始化
   */
  init() {
    editor.init();
    panZoom.init();
    contentMenu.init();
  },

  /**
   * @description  缩小画布
   */
  zoomIn() {
    const x = instance.mainContainerWrap.clientWidth / 2;
    const y = instance.mainContainerWrap.clientHeight / 2;
    instance.pan.smoothZoom(x, y, 0.8);
  },

  /**
   * @description 放大画布
   */
  zoomOut() {
    const x = instance.mainContainerWrap.clientWidth / 2;
    const y = instance.mainContainerWrap.clientHeight / 2;
    instance.pan.smoothZoom(x, y, 1.2);
  },

  /**
   * @description 加载数据
   */
  loadData(data) {
    var nodes = this.getData().nodes;
    instance.deleteEveryConnection();
    instance.deleteEveryEndpoint();
    for (let index = 0; index < nodes.length; index++) {
      const element = nodes[index];
      instance.remove(element.id);
    }

    model.setData(data);
    editor.render();
  },

  getInstance() {
    return instance;
  },

  /**
   * @description 执行模型
   * @returns {Promise}
   */
  execModel() {
    return editor.execModel();
  },
  changeStateByNodeId(nodeId, state) {
    editor.changeStateByNodeId(nodeId, state);
  },
  blingConnectors(edges) {
    editor.blingConnectors(edges);
  }
};

extendsEvent(FlowChart);

document.addEventListener("click", ev => {
  EventCenter.emit("document.click", ev);
});

export default FlowChart;
